using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using Common.Utilities;
using Common.Utilities.Dispatcher;
using Workflows.Components.DALC;

namespace Workflows.Components
{
    /// <summary></summary>
    public class GlobalVariableGetterWorker:IWorker
    {
        private Thread _WorkerThread;
        private GlobalVariableGetterInstruction _Instruction;
        private GlobalVariableGetterOutput _Output;
        private Guid _TicketID;
        private RunContext _Context;
        private string _WorkflowName;
        private string _ComponentName;

        /// <summary>
        /// 
        /// </summary>
        public string ComponentName
        {
            get { return _ComponentName; }
        }

        /// <summary>
        /// 
        /// </summary>
        public string WorkflowName
        {
            get { return _WorkflowName; }
        }

        /// <summary></summary>
        public Thread WorkerThread
        {
            get { return _WorkerThread; }
        }

        /// <summary></summary>
        public InstructionBase Instruction
        {
            get { return _Instruction; }
        }

        /// <summary></summary>
        public ResultBase Output
        {
            get { return _Output; }
        }

        /// <summary></summary>
        public Type ComponentType
        {
            get { return typeof(GlobalVariableGetterComponent); }
        }

        /// <summary></summary>
        public RunContext Context
        {
            get { return this._Context; }
            set { this._Context = value; }
        }

        /// <summary></summary>
        public void SetInstruction(Guid ticketGuid, InstructionBase request, string workflowName, string componentName)
        {
            this._TicketID = ticketGuid;
            this._Instruction = (GlobalVariableGetterInstruction) request;
            this._WorkflowName = workflowName;
            this._ComponentName = componentName;
        }

        /// <summary></summary>
        public void SetInstruction(RunContext context, Guid ticketGuid, InstructionBase request, string workflowName, string componentName)
        {
            this._Context = context;
            this._TicketID = ticketGuid;
            this._Instruction = (GlobalVariableGetterInstruction) request;
            this._WorkflowName = workflowName;
            this._ComponentName = componentName;
        }

        /// <summary></summary>
        public void DoWork()
        {
            if (this.WorkerStartedEvent != null)
                this.WorkerStartedEvent(this._TicketID, this, this.GetType(), this._Instruction);

            this._WorkerThread = Thread.CurrentThread;
            this.RetrieveGlobalVariables();
        }

        /// <summary></summary>
        public void DoWorkAsync()
        {
            if (this.WorkerStartedEvent != null)
                this.WorkerStartedEvent(this._TicketID, this, this.GetType(), this._Instruction);

            this._WorkerThread = new Thread(new ThreadStart(this.RetrieveGlobalVariables));
            this._WorkerThread.Start();
        }

        /// <summary></summary>
        public void CancelWork()
        {
            if(this._WorkerThread !=null && this._WorkerThread.IsAlive)
            {
                this._WorkerThread.Abort();
                this._WorkerThread.Join();

                if (this.WorkerCancelledEvent != null)
                    this.WorkerCancelledEvent(this._TicketID, this.GetType(), this._Instruction);
            }
        }

        public event WorkerStarted WorkerStartedEvent;
        public event WorkerFinished WorkerFinishedEvent;
        public event WorkerHasError WorkerErredEvent;
        public event WorkerCancelled WorkerCancelledEvent;
        public event WorkerStatusChanged WorkerStatusChangedEvent;

        #region work
        private void RetrieveGlobalVariables()
        {
            try
            {
                this._Output = new GlobalVariableGetterOutput(this._Instruction.OwnerComponentID, this._TicketID, null);
                if(this._Instruction.GlobalVariableNames !=null && this._Instruction.GlobalVariableNames.Count>0)
                {
                    this._Output.GlobalVars=new List<GlobalVariable>();
                    foreach(string gVarName in this._Instruction.GlobalVariableNames)
                    {
                        GlobalVariable gVar=new GlobalVariable();
                        gVar.VarName = gVarName;
                        gVar.DataType = SimpleDataType.TextType;
                        gVar.VarValue = string.Empty;
                        if(this._Context !=null && this._Context.Vars!=null && this._Context.Vars.Count>0)
                        {
                            foreach(GlobalVariable gVar1 in this._Context.Vars)
                            {
                                if(gVar1.VarName==gVar.VarName)
                                {
                                    gVar = gVar1;
                                }
                            }
                        }
                        this._Output.GlobalVars.Add(gVar);
                    }
                }
                if (this._Context != null && this._Context.IsSubflow)
                {
                    
                }
                else
                {
                    // run global variable is saved in DB
                    Run run = RunDALC.GetRunByJob(this._TicketID);
                    if(run.Vars !=null && run.Vars.Count>0 && this._Output.GlobalVars !=null && this._Output.GlobalVars.Count>0)
                    {
                        for (int k = 0; k < this._Output.GlobalVars.Count; k++)
                        {
                            GlobalVariable gVar = this._Output.GlobalVars[k];
                            for (int i = 0; i < run.Vars.Count; i++)
                            {
                                if (run.Vars[i].VarName == gVar.VarName)
                                {
                                    gVar = run.Vars[i];
                                    this._Output.GlobalVars[k] = gVar;
                                    break;
                                }
                            }
                        }
                    }
                }

                if(this.WorkerFinishedEvent !=null)
                {
                    this.WorkerFinishedEvent(this._TicketID, this.GetType(), this._Instruction, this._Output);
                }
            }
            catch(Exception ex)
            {
                if (this.WorkerErredEvent != null)
                    this.WorkerErredEvent(this._TicketID, this.GetType(), this._Instruction, ex.ToString(),true);
            }
        }
        #endregion
    }
}
